package com.wise.service.cms.content.column;

import cn.hutool.core.collection.CollectionUtil;
import com.wise.entities.content.Article;
import com.wise.entities.content.ArticleColumn;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoRemove;
import com.wise.framework.exception.LogicException;
import com.wise.summer.v1.cms.content.column.DeleteColumnById;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.List;
import java.util.stream.Collectors;

import static com.wise.model.content.ContentErrMsgs.Err_001600250014;
import static com.wise.model.content.ContentErrMsgs.Err_001600250022;

@Service
@SummerService
public class DeleteColumnService implements SummerServiceBean<DeleteColumnById> {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void sum(DeleteColumnById model) {
        ArticleColumn articleColumn = DaoOne4Update.s(ArticleColumn.class, model.getId(), Err_001600250014);
        DaoRemove.s(articleColumn);
        //删除对应文章
        SqlSelect sql = SqlSelect.selec("*");
        sql.fromClassName(Article.class);
        sql.eq(Article._columnId, model.getId());
        List<Article> articleList = DaoList.s(Article.class, sql);
        List idList = articleList.stream().map(Article::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(idList)) {
//            MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
//            mapSqlParameterSource.addValue("id", idList);
//            namedParameterJdbcTemplate.update("delete from knowledge_article where id in (:id)", mapSqlParameterSource);
            throw new LogicException(this.getClass(), Err_001600250022);
        }
        model.setSummerResult(Boolean.TRUE);
    }
}
